package tr.edu.metu.is.mega.server.business;

import java.util.List;

import tr.edu.metu.is.mega.common.dto.CourseDTO;
import tr.edu.metu.is.mega.common.dto.LectureNoteDTO;
import tr.edu.metu.is.mega.server.dao.LectureNoteDAO;
import tr.edu.metu.is.mega.server.entity.Lecturenote;

/**
 * Class to manage lecture notes.
 */
public class LectureNoteManager {

    private final LectureNoteDAO dao;

    private final LectureNoteDTOHelper helper;

    /**
     * Default constructor.
     */
    public LectureNoteManager() {
        dao = new LectureNoteDAO();
        helper = new LectureNoteDTOHelper();
    }

    /**
     * Creates a lecture note.
     * 
     * @param lectureNote lecture note
     * @return lecture note
     */
    public LectureNoteDTO createLectureNote(LectureNoteDTO lectureNote) {
        Lecturenote entity = new Lecturenote();
        helper.convertToEntity(lectureNote, entity);
        return helper.convertToDTO(dao.create(entity));
    }

    /**
     * Updates a lecture note.
     * 
     * @param lectureNote lecture note
     * @return lecture note
     */
    public LectureNoteDTO updateLectureNote(LectureNoteDTO lectureNote) {
        Lecturenote entity = dao.findById(lectureNote.getPkid());
        helper.convertToEntity(lectureNote, entity);
        return helper.convertToDTO(dao.update(entity));
    }

    /**
     * Deletes a lecture note.
     * 
     * @param lectureNote lecture note
     */
    public void deleteLectureNote(LectureNoteDTO lectureNote) {
        dao.deleteById(lectureNote.getPkid());
    }

    /**
     * Returns list of lecture notes in a course.
     * 
     * @param course course
     * @return list of lecture notes
     */
    public List<LectureNoteDTO> getLectureNotes(CourseDTO course) {
        LectureNoteDTO lectureNote = new LectureNoteDTO();
        lectureNote.setCourse(course);

        Lecturenote entity = new Lecturenote();

        helper.convertToEntity(lectureNote, entity);

        List<Lecturenote> examples = dao.findByExample(entity, false);

        return helper.toDTO(examples);
    }

}
